/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
var lottery={
    index:-1,	//当前转动到哪个位置，起点位置
    count:0,	//总共有多少个位置
    timer:0,	//setTimeout的ID，用clearTimeout清除
    speed:20,	//初始转动速度
    times:0,	//转动次数
    cycle:50,	//转动基本次数：即至少需要转动多少次再进入抽奖环节
    prize:-1,	//中奖位置
    init:function(id){
        if ($("#"+id).find(".lottery-item").length>0) {
            $lottery = $("#"+id);
            $items = $lottery.find(".lottery-item");
            this.obj = $lottery;
            this.count = $items.length;
            //$lottery.find(".lottery-item-"+this.index).addClass("active");
        };
    },
    roll:function(){
        var index = this.index;
        var count = this.count;
        var lottery = this.obj;
        $(lottery).find(".lottery-item-"+index).removeClass("active");
        index += 1;
        if (index > count-1) {
            index = 0;
        };
        $(lottery).find(".lottery-item-"+index).addClass("active");
        this.index = index;
        return false;
    },
    stop:function(index){
        this.prize = index;
        return false;
    }
};
function roll(index){
    lottery.times += 1;
    lottery.roll();
    if (lottery.times > lottery.cycle + 10 && lottery.prize == lottery.index) {
        if(lottery.isWinning){//中奖了
            //弹出提示框
            $(".popup-box").children().find("p").html($("#lottery").attr("prize_title"));
            $(".popup-box").children().find("img").attr("src",$("#lottery").attr("prize_img"));
            $(".popup-box").delay(500).fadeIn(200);
        }
        clearTimeout(lottery.timer);
        lottery.prize = -1;
        lottery.times = 0;
        lottery_click = false;
    }else{
        if (lottery.times < lottery.cycle) {
            lottery.speed -= 10;
        }else if(lottery.times == lottery.cycle) {
            lottery.prize = index;	
        }else{
            if (lottery.times > lottery.cycle + 10 && ((lottery.prize == 0 && lottery.index == 7) || lottery.prize == lottery.index + 1)) {
                lottery.speed += 110;
            }else{
                lottery.speed += 20;
            }
        }
        if (lottery.speed < 40) {
            lottery.speed = 40;
        };
        lottery.timer = setTimeout("roll("+index+")",lottery.speed);
    }
    return false;
}
var lottery_click = false;

$(function(){
    lottery.init('lottery');
    $("#lottery a.go").click(function(){
        if(lottery_click){//不可点击
            return false;
        }else{//执行抽奖操作
            lottery.speed = 100;
            try{
                var reqStruct = {
                    url:"/lucky/lottery.html",async:false,type:"post",dataType:"json",data:{sptime:Math.random()},timeout:60000,
                    success:function(data){
                        if(data.errorCode == 1001){//未登录
                            layer.confirm(data.errorMsg,{btn:['立即登录'],offset:300,icon:0,title:"小6提示"},function(){
                                layer.closeAll();showLogin("login");
                            });
                            return false;
                        }else if(data.errorCode == 1004){//没有抽奖机会了
                            layer.msg(data.errorMsg,{icon:5,offset:300,time:2000});
                            return false;
                        }else if(data.errorCode == 1000){//中奖了
                            $("#lottery").attr("prize_item",data.prize_item);
                            $("#lottery").attr("prize_title",data.prize_title);
                            $("#lottery").attr("prize_img",data.prize_img);
                            lottery.isWinning = true;
                            lottery_click = true;
                            roll(data.prize_item);
                        }
                    }
                };
                $.ajax(reqStruct);
            }catch(e){}
            return false;
        }
    });
});

